#include "ohos_audio_manager.h"
#include <cstdint>
#include "rtc_base/logging.h"
#include "pc/peer_connection_factory.h"
#include "pc/peer_connection_factory_proxy.h"
#include "audio/audio_state.h"

namespace webrtc {

OHOSAudioManager::OHOSAudioManager(PeerConnectionFactoryInterface* pc_factory, rtc::Thread *worker_thread) 
    : pc_factory_(pc_factory), worker_thread_(worker_thread) {
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Ctor";
}

OHOSAudioManager::~OHOSAudioManager(){
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Dtor";
}

int32_t OHOSAudioManager::Mute() {
  RTC_LOG(LS_INFO) << "OHOSAudioManager: Mute";
  RTC_DCHECK(worker_thread_);
  if (worker_thread_) {
    return worker_thread_->BlockingCall([&] {
      webrtc::PeerConnectionFactoryProxyWithInternal<webrtc::PeerConnectionFactory>* factory =
      static_cast<webrtc::PeerConnectionFactoryProxyWithInternal<webrtc::PeerConnectionFactory>*>(pc_factory_);
      webrtc::internal::AudioState* audio_state = (webrtc::internal::AudioState*)(factory->internal()->media_engine()->voice().GetAudioState().get());
      return audio_state->MuteRecordedData(true);
    });
  }
}

int32_t OHOSAudioManager::UnMute() {
  RTC_LOG(LS_INFO) << "OHOSAudioManager: UnMute";
  RTC_DCHECK(worker_thread_);
  if (worker_thread_) {
    return worker_thread_->BlockingCall([&] {
      webrtc::PeerConnectionFactoryProxyWithInternal<webrtc::PeerConnectionFactory>* factory =
      static_cast<webrtc::PeerConnectionFactoryProxyWithInternal<webrtc::PeerConnectionFactory>*>(pc_factory_);
      webrtc::internal::AudioState* audio_state = (webrtc::internal::AudioState*)(factory->internal()->media_engine()->voice().GetAudioState().get());
      return audio_state->MuteRecordedData(false);
    });
  }
}

}
